<template>
  <el-dialog
    v-model="visible"
    title="检测结果详情"
    width="90%"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    class="result-dialog"
    @closed="handleClosed"
  >
    <div v-loading="loading" class="result-container">
      <div class="task-info">
        <div class="task-info-item">
          <span class="label">任务名称:</span>
          <span class="value">{{ taskInfo.name }}</span>
        </div>
        <div class="task-info-item">
          <span class="label">目标IP:</span>
          <span class="value">{{ taskInfo.ip }}</span>
        </div>
        <div class="task-info-item">
          <span class="label">端口范围:</span>
          <span class="value">{{ taskInfo.ports }}</span>
        </div>
        <div class="task-info-item">
          <span class="label">状态:</span>
          <el-tag :type="getStatusType(taskInfo.status_info)">{{
            taskInfo.status_info
          }}</el-tag>
        </div>
      </div>

      <div class="result-actions">
        <el-button
          type="primary"
          :icon="Download"
          @click="handleDownloadReport"
        >
          下载检测报告
        </el-button>
      </div>

      <div v-if="taskList.length" class="vuln-list">
        <base-table
          :data="taskList"
          :total="total"
          :page="queryParams.page_index"
          :limit="queryParams.page_size"
          :page-sizes="[10, 20, 50, 100]"
          :loading="loading"
          :empty-text="'暂无检测任务'"
          @pagination="handlePagination"
        >
          <el-table-column prop="ip" label="目标IP" width="120" />
          <el-table-column prop="Port" label="目标端口" width="90" />
          <el-table-column prop="Port_Protocol" label="端口协议" width="90" />
          <el-table-column prop="CVSS" label="严重程度" width="90">
            <template #default="{ row }">
              <el-tag :type="getCvssTag(row.CVSS)">{{ row.CVSS }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="NVT_Name" label="检测工具">
            <template #default="{ row }">
              <el-tooltip :content="row.NVT_Name" placement="top" :show-after="1000">
                <span class="ellipsis">{{ row.NVT_Name }}</span>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column
            prop="Specific_Result"
            label="检测结果"
          >
         <template #default="{ row }">
              <el-tooltip :content="row.Specific_Result" placement="top" :show-after="1000">
                <span class="ellipsis">{{ row.Specific_Result }}</span>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column prop="Timestamp" label="检测时间" width="120" />
        </base-table>
      </div>

      <el-empty
        v-else-if="!resultLoading && !taskList.length"
        description="暂无检测结果"
      />
    </div>
  </el-dialog>
</template>

<script setup>
/**
 * 检测结果弹窗组件
 * @module ResultDialog
 * @description 用于显示漏洞检测结果的弹窗组件
 */
import { Download } from "@element-plus/icons-vue";
import { getTaskDetail } from "@/api/vulnDetection";
// 接收参数
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  taskInfo: {
    type: Object,
    default: () => ({}),
  },
  loading: {
    type: Boolean,
    default: false,
  },
});

// 发出事件
const emit = defineEmits([
  "update:modelValue",
  "download-report",
  "refresh",
  "closed",
]);
// 查询参数
const queryParams = reactive({
  page_index: 1,
  page_size: 10,
});
const total = ref(0);
// 组件状态
const visible = computed({
  get: () => props.modelValue,
  set: (val) => emit("update:modelValue", val),
});
const resultLoading = ref(false);
const taskList = ref([]);

// 获取状态类型
const getCvssTag = (score) => {
  score = parseInt(score);
  if (score == 0) {
    return "success"
  }
  return "warning"
};

// 获取状态类型
const getStatusType = (status) => {
  const typeMap = {
    已完成: "success",
    进行中: "primary",
    等待中: "info",
    失败: "danger",
    异常: "warning",
  };

  for (const key in typeMap) {
    if (status && status.includes(key)) {
      return typeMap[key];
    }
  }

  return "";
};

watch(
  () => props.taskInfo,
  (newValue, oldValue) => {
    handleViewResult();
  }
);

// 处理查看结果
const handleViewResult = async (id) => {
  let res = await getTaskDetail({
    id: props.taskInfo.id,
    ...queryParams,
  });
  console.log(res);
  taskList.value = res.data;
  total.value = res.total_items;
};

// 下载检测报告
const handleDownloadReport = () => {
  if (!props.taskInfo.id) {
    ElMessage.warning("无效的任务ID");
    return;
  }

  emit("download-report", props.taskInfo.id);
};
// 处理分页
const handlePagination = ({ page, limit }) => {
  queryParams.page_index = page;
  queryParams.page_size = limit;
  handleViewResult();
};

// 关闭弹窗处理
const handleClosed = () => {
  emit("closed");
};
</script>

<style lang="scss" scoped>
.result-dialog {
  :deep(.el-dialog__body) {
    padding: 20px;
  }
}

.result-container {
  min-height: 400px;
}

.task-info {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin: 15px 0;

  .task-info-item {
    display: flex;
    align-items: center;
    margin-right: 20px;
    .label {
      font-weight: bold;
      margin-right: 8px;
      color: var(--text-color-secondary);
    }

    .value {
      color: var(--text-color);
    }
  }
}

.result-actions {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;

  .search-input {
    width: 300px;
  }
}

.vuln-list {
  margin-top: 20px;
}
</style>
